#include <fstream>
#include <iostream>
#include <string.h>
#include <sstream>
#include <vector>

struct Data {
    int val[3];
    std::vector<int> arr;
};


void get_data(const std::string& file, std::vector<Data>& data, int& count) {
    std::ifstream fin(file, std::ios::in);

    if (!fin.is_open()) {
        std::cerr << "File open error!" << std::endl;
        return;
    }

    std::string line;

    std::getline(fin, line);
    count = std::atoi(line.c_str());
    printf("line num = %d\n", count);

    while (std::getline(fin, line)) {
        // printf("%s \n", line.c_str());
        Data item;
        std::istringstream iss(line);
        int val;
        for (int i = 0; i < 3; i++) {
            iss >> val;
            item.val[i] = val;
        }

        std::getline(fin, line);
        // printf("%s \n", line.c_str());
        std::istringstream oss(line);
        while (oss >> val) {
            item.arr.push_back(val);
        }
        data.push_back(item);
    }
}


void write_data(const std::string& file, std::vector<Data>& data, int count) {
    std::ofstream fout(file, std::ios::out);
    if (!fout.is_open()) {
        std::cerr << "File Open Error!" << std::endl;
        return;
    }

    fout << std::to_string(count) + "\n";
    for (auto& item : data) {
        fout << std::to_string(item.val[0]) + " " +  std::to_string(item.val[1]) + 
                " " +  std::to_string(item.val[2]) + "\n";
        std::string line;
        for (auto& val : item.arr) {
            line += std::to_string(val) + " ";
        }
        line += "\n";
        fout << line;
    }

    fout.close();
    return;
}

int main() {
    std::string file = "./meow/meow3.in";
    std::vector<Data> data;

    int count = 0;
    get_data(file, data, count);

    // for (auto& item : data) {
    //     printf("%d  %d  %d  //  ", item.val[0], item.val[1], item.val[2]);
    //     printf("%d  %d  %d\n", item.arr[0], item.arr[1], item.arr[2]);
    //     printf("====================\n");
    // }
    write_data("./tmp.txt", data, count);

    return 0;
}